<!-- Author @ Atindriyo Sanyal -->
<!-- Web Interface for Querying our database  -->
<!-- Database connection information -->
<!-- Server: localhost-->
<!-- DB User : cs143  -->
<!-- Password : NO PASSWORD  -->
<!-- DB Name : CS143  -->


<html>
    <body>
    	<span>-----Web interface-----</span><br/>
    	<span>Name: Atindriyo Sanyal</span><br/>
    	<span>UID : 303-986-288</span><br/>
        <span>Type an SQL query in the following box:</span>
        <br/><br/>
        <form method="post">
            <textarea name="query-text" style="height:180px; width:450px;"><?php if(isset($_POST['submit']))echo $_POST['query-text'];?></textarea>
            <input type="submit" name="submit" value="Run Query"></input>
        </form>
        <span>Note: tables and fields are case sensitive. Run "show tables" to see the list of available tables.</span>
        <?php
       
        /**
         *@method: To connect to the Database
         *@author: Atin 
         */
        function DBConnect(){
            
            $dbhost = 'localhost';
            $dbuser = 'cs143';
            $dbpass = '';
            $dbname = 'CS143';
            $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
            mysql_select_db($dbname);
   
        }
        
        /*
         * @method : To run the query given by the user
         * @author : Atin
         */
        function RunQuery($query) {
            
            DBConnect();
            
            $result = mysql_query($query);

            $num_rows = mysql_num_rows($result);
	    $num_fields = mysql_num_fields($result);

            if (!$result) {
                $message = '<br/>Invalid query: ' . mysql_error() . "<br/>";
                $message .= 'Whole query: ' . $query;
                die($message);
            } else if ($num_rows == 0) {
                echo "Query Returned no rows. Please try a different query!";
            } else {
                echo "<h2> Results </h2>";
                echo "<table border='1'>";

                
                for($i=0;$i<$num_fields;$i++) {

                    $field_name = mysql_field_name($result, $i);
		    echo "<td align='center'>" . $field_name . "</td>";
                    $field_offset++;
                }


                while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

                    echo "<tr>";
                  

                for($i=0;$i<$num_fields;$i++) {

                $field_name = mysql_field_name($result, $i);
                if($row[$field_name] == '0000-00-00'){
				$row[$field_name] = 'N/A';
				}
			
				echo "<td align='center'>" . $row[$field_name] . "</td>";
                $field_offset++;
                        
               }

                    echo "</tr>";
                    
             }

                echo "</table>";
            }

            mysql_free_result($result);
        }

        if ($_POST['submit']) {
            RunQuery($_POST['query-text']);
        }
        ?>
    </body>
</html>
